package com.example.userpage.service;

import com.example.userpage.model.ImageUploadRequest;
import com.example.userpage.model.ResponseMessage;
import com.example.userpage.repository.UserRepository;
import com.example.userpage.repository.votes.ImageRepository;
import com.example.userpage.model.User;
import com.example.userpage.model.Image;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Optional;

@Service
public class ImageUploadService {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private com.example.userpage.repository.votes.ImageRepository imageRepository; // 操作 votes.images

    public ResponseMessage handleImageUpload(String ip, ImageUploadRequest request, MultipartFile file) throws IOException {
        if ("user".equals(request.getImageClass())) {
            return handleUserImageUpload(ip, file);
        } else if ("character".equals(request.getImageClass())) {
            return handleCharacterImageUpload(ip, request, file);
        }
        return new ResponseMessage("Invalid image class");
    }

    private ResponseMessage handleUserImageUpload(String ip, MultipartFile file) throws IOException {
        try {
            User user = userRepository.findFirstByIp(ip);
            if (user != null) {
                // Save the image into the user's profile
                user.setImage(file.getBytes());
                userRepository.save(user);
                return new ResponseMessage("上传成功！");
            }
            return new ResponseMessage("上传失败！找不到用户");
        } catch (IOException e) {
            // 记录异常日志
            e.printStackTrace();
            return new ResponseMessage("上传失败！处理文件时出错");
        }
    }

    private ResponseMessage handleCharacterImageUpload(String ip, ImageUploadRequest request, MultipartFile file) throws IOException {
        try {
            User user = userRepository.findFirstByIp(ip); // 通过 IP 查询用户
            if (user != null) {
                // 查询图片是否存在
                Optional<Image> existingImage = imageRepository.findByImageName(request.getImageName());
                if (existingImage.isPresent()) {
                    // 如果角色名已经存在，返回错误消息
                    return new ResponseMessage("角色名已存在，请重新上传和命名！");
                }

                // 创建一个新的 Image 对象
                Image image = new Image();
                image.setImageName(request.getImageName());
                image.setImageData(file.getBytes()); // 存储二进制图片数据
                image.setVoteCount(0);
                image.setCreator(user.getUsername()); // 设置创建者用户名
                imageRepository.save(image); // 保存图片数据

                return new ResponseMessage("上传成功！");
            }
            return new ResponseMessage("上传失败！找不到用户");
        } catch (IOException e) {
            // 记录异常日志
            e.printStackTrace();
            return new ResponseMessage("上传失败！处理文件时出错");
        }
    }

}
